package org.example.admin.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wf.captcha.SpecCaptcha;
import org.example.admin.service.UserSystemService;
import org.example.entity.UserSystem;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/login")
public class UserSystemLogin {

    @Resource
    private UserSystemService userSystemService;

    @GetMapping("/code")
    public void code(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //生产图片验证码
        SpecCaptcha specCaptcha = new SpecCaptcha(100,38,4);
        //创建session域存进去
        HttpSession session = req.getSession();
        session.setAttribute("code",specCaptcha.text());
        //响应给客户端
        specCaptcha.out(resp.getOutputStream());
    }

    @PostMapping("/uLogin")
    public void userLogin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //接受参数
        String username =req.getParameter("username");
        String passwrod =req.getParameter("password");
        String code = req.getParameter("code");

        //session域
        HttpSession session = req.getSession();
        try{
            //参数校验
            paramsCheckout(username,passwrod);
        }catch(RuntimeException e){
            //获取异常的信息
            String message = e.getMessage();
            session.setAttribute("error",message);
            resp.sendRedirect("/admin/login.jsp");
//            return;
        }
        //获取验证码的session
        Object obj = session.getAttribute("code");
        //类型转换
        String codeStr = (String) obj;

        //如果不为空，查询数据库是否存在
        try {

            UserSystem one = userSystemService.getOne(new QueryWrapper<>(new UserSystem(username, passwrod)));

            //判断数据不等于空 并且 验证码输入正确
            if(one != null && codeStr.equalsIgnoreCase(code)){
                //如果不等于空就表示能获取到数据
                //用于过滤器
                session.setAttribute("userSession",one);

                //跳转页面
                resp.sendRedirect("/admin/userList.jsp");
            }else if( !codeStr.equalsIgnoreCase(code)){
                //表示验证码输入错误
                String message = "验证码错误";
                session.setAttribute("error",message);
                resp.sendRedirect("/admin/login.jsp");
            }else{
                //等于空，表示密码或者用户名错误
                String message = "用户名或者密码错误";
                session.setAttribute("error",message);
                resp.sendRedirect("/admin/login.jsp");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void paramsCheckout(String userName,String passwrod){
        //判断用户名是否输入了
        if(userName == null || userName.equals(" ")){
            throw new RuntimeException("用户名未填");
        }
        //判断密码是否输入了
        if(passwrod == null || passwrod.equals(" ")){
            throw new RuntimeException("密码未填");
        }
    }


}
